package com.soulless.singleton;

import java.util.List;

/**
 * 单例下的原型模式
 *
 * <p>
 * 【Cloneable】破坏了单例，如果原型对象是单例就不应该实现Cloneable接口
 * 如果非要实现clone接口，clone方法直接：return instance
 * 所以原型模式与单例模式不可同时存在
 * </p>
 *
 * @author zenglei
 * @date 2022/11/17 10:12
 */
public class ConcretePrototype implements Cloneable {

    private int age;
    private String name;
    private List<String> hobbies;

    private static ConcretePrototype instance = new ConcretePrototype();

    private ConcretePrototype() {}

    public static ConcretePrototype getInstance() {
        return instance;
    }

    public List<String> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "ConcretePrototype{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", hobbies=" + hobbies +
                '}';
    }

//    public ConcretePrototype clone() {
//        try {
//            return (ConcretePrototype) super.clone();
//        } catch (CloneNotSupportedException e) {
//            e.printStackTrace();
//            return null;
//        }
//    }

    /**
     * 当原型对象为单例时，同时实现了【Cloneable】接口，为了不破坏单例此方法应该如下
     * @return
     */
    public ConcretePrototype clone() {
        return instance;
    }

}
